<?php $this->load->view("admin/$adminCurrentTemplate/commonHead2"); ?>
<table width="99%" border="0" cellspacing="1" cellpadding="0"
       class="editFormContainer">
    <form id="editForm" name="editForm" method="post"
          action="<?= $this->input->server('REQUEST_URI') ?>" encType="multipart/form-data" runat="server">
        <input type="hidden" name="isadmin" value="1">
        <input type="hidden"
               name="id" id="id" value="<?php if (isset($id))
    echo $id; ?>" />
        <tr>
            <td class="guid">分类名称：</td>
            <td class="formItem">
                <input type="text" name="name" value="<?php if(isset($name)) echo $name;?>"/>
            </td>
        </tr>
        <tr>
            <td class="guid">上级分类：</td>
            <td class="formItem">
                <select name="pid" id="note_id" size="1" class="single-select"><?php
                                        $str = '';
                                        foreach ($note as $value)
                                        {
                                            $str .= "<option value=\"" . $value->id . "\"";
                                            if ($value->id == $note_id)
                                            {
                                                $str .= " selected=\"selected\"";
                                            }
                                            $str .=">";
                                            for ($i = 0; $i < $value->level; $i++)
                                            {
                                                $str .= "&nbsp;|-";
                                            }
                                            $str .= $value->name . "</option>\n";
                                        }
                                        echo $str;
                                        ?></select></td>
        </tr>
        <tr>
            <td class="guid">图片：</td>
            <td class="formItem">
               <input type="file" name="img"/>
            <?php if(isset($img))
                echo "<img src='".$img."'>";
                ?>
            </td>
        </tr>
        
    </form>
</table>
<?php $this->load->view("admin/$adminCurrentTemplate/commonFoot2"); ?>
<script type="text/javascript">
    var tvName = '<?php if (isset($tvName))
    echo $tvName; ?>';
        $(function(){
            $('#userName').bind('blur',userNameBlur);
            var id = $('#id').val();
            if(id.length==0) dg.SetTitle('添加产品');
            dg.addBtn('subBtn','提 交',subForm);
            $('#note_id').bind('change',groupChange).change()
        })

        var userNameBlur = function(){
            var id = $.trim($('#id').val());
            var userName = $.trim($('#userName').val());
            $.ajax({
                type:'POST',
                url:getInfo + '/check/',
                data:'tvName=' + tvName + '&fieldName=name&fieldValue=' + userName + '&id=' + id,
                success:function(returnValue){
                    if(parseInt(returnValue)>0){
                        subMess(310,108,'产品名称','<span class="red">对不起，产品名称已经存在,请选择其它产品名称！</span>',$("#userName"));
                        return false;
                    }
                }
            });
        }

        var groupChange = function(){
            var thisValue = $(this).val();
            $.ajax({
                type:'POST',
                url:getInfo + '/../permission',
                data:'adminGroupParentId=' + thisValue,
                success:function(returnValue){
                    var permissions = $('#permissions').val();
                    $('.permission-container').html(returnValue);
                    $('.permission-container .permission-0').each(function(){
                        var thisId = $(this).children().eq(0).attr('id');
                        if($('.' + thisId).length==0) $(this).remove();
                    });
                    $('.permission-container input[name="p_all"]').bind('click',p_all);
                    $('.permission-container input[name^="permission"]').bind('click',p_click);
                    if(permissions.length>0){
                        permissions = getJSON(permissions);
                        $('.permission-container input[name^="permission"]').each(function(){
                            var thisV = $(this).val().split('_');
                            var v_key = thisV[0];
                            var v_val = thisV[1];
                            try{
                                if(permissions[v_key][v_val]){
                                    $(this).click();
                                }
                            }catch(e){}
                        });
                    }
                }
            });
        }

        var p_all = function(){
            var thisId = $(this).attr('id');
            if(this.checked){
                $('.' + thisId).children().attr('checked',true);
            }else{
                $('.' + thisId).children().removeAttr('checked');
            }
        }

        var p_click = function(){
            var thisValue = $(this).val();
            var parentId = $(this).parents('div').attr('class');
            var inputLength = $('.' + parentId + ' input').length;
            var checkedLength = $('.' + parentId + ' input:checked').length;
            if(inputLength==checkedLength){
                $('#' + parentId).attr('checked',true);
            }else{
                $('#' + parentId).removeAttr('checked');
            }
        }

        var subForm = function(){
            var id = $.trim($('#id').val());
            var disabled = $.trim($('#disabled').val());
            var userName = $.trim($('#userName').val());
            var userPWD = $.trim($('#userPWD').val());
            var userPWD2 = $.trim($('#userPWD2').val());


            var permissions = perms = '';
            $('.permission-container input[name^="permission"]:checked').each(function(i){
                var thisV = $(this).val().split('_');
                var v_key = thisV[0];
                var v_val = thisV[1];
           
                perms = v_key;

            });
            $('#permissions').val(permissions);
            $('.permission-container .permission-ul').remove();
	
            $('#editForm').submit();
        }
</script>
